package com.yeraphael.service.impl;

import com.yeraphael.entity.Emp;
import com.yeraphael.entity.LoginData;
import com.yeraphael.mapper.LoginMapper;
import com.yeraphael.service.LoginService;
import com.yeraphael.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;


    @Override
    public LoginData login(Emp emp) {
        Emp empDB = loginMapper.login(emp);

        if (empDB != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", empDB.getId());
            claims.put("username", empDB.getUsername());

            String jwt = JWTUtils.generateJWT(claims);

            return new LoginData(empDB.getId(), empDB.getUsername(), empDB.getName(), jwt);
        }

        return null;
    }
}
